import * as api from '@/services/PriceMgr';
import * as utils from '../utils/treeUtils';
import _ from 'lodash';

export default {
  namespace: 'GROUP_APPROVE_VERSION',
  state: {
    all: [],
    emptyTitle: '暂无集团审批版本数据',
    versionLoading: false,
    dataSource: [],
    expandedRowKeys: [],
    versionList: [], // 版本列表
    titleTree: utils.titleTree,
    fixedColumns: utils.fixedColumns(),
    coreIndicatorsLoading: false,
    coreIndicatorsVisible: false,
    coreIndicatorsData: [],
  },
  effects: {
    *getVersionList({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          versionLoading: true,
          emptyTitle: '数据加载中...',
          all: [],
          expandedRowKeys: [],
          dataSource: [],
        },
      });
      let versionList = [];
      const response = yield call(api.priceGroupPriceVersionList, payload);

      if (response && response.code === 1 && !_.isEmpty(response.data)) {
        versionList = response.data;
      }
      yield put({
        type: 'save',
        payload: {
          versionLoading: false,
          emptyTitle: '暂无集团审批版本数据',
          versionList,
        },
      });

      if (callback && !_.isEmpty(versionList)) callback(_.head(versionList));
    },
    *getData({ payload }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          emptyTitle: '数据加载中...',
          all: [],
          expandedRowKeys: [],
          dataSource: [],
        },
      });
      let all = [];
      let expandedRowKeys = [];
      let dataSource = [];
      const response = yield call(api.priceGroupPriceVersion, payload);

      if (response && response.code === 1 && !_.isEmpty(response.data)) {
        all = _.cloneDeep(response.data);
        expandedRowKeys = utils.getExpandedRowKeys(all);
        dataSource = utils.buildTree(all);
      }
      yield put({
        type: 'save',
        payload: {
          emptyTitle: '暂无集团审批版本数据',
          all,
          expandedRowKeys,
          dataSource,
        },
      });
    },
    *handlerCoreIndicatorsVisible(__, { put, select }) {
      const { coreIndicatorsVisible, dataSource } = yield select(
        state => state.GROUP_APPROVE_VERSION,
      );
      // 是打开状态
      if (!coreIndicatorsVisible) {
        yield put({
          type: 'save',
          payload: {
            coreIndicatorsVisible: true,
            coreIndicatorsLoading: true,
          },
        });

        const originalData = _.cloneDeep(dataSource[0]);
        const coreIndicatorsData = [
          {
            key: 'equityPrice',
            name: '权益前货值（万元）',
            precision: 0,
          },
          {
            key: 'equityBeforeProfit',
            name: '权益前创造利润额（万元）',
            precision: 0,
          },
          {
            key: 'equityAfterProfit',
            name: '权益后创造利润额（万元）',
            precision: 0,
          },
          {
            key: 'createProfit',
            name: '创造利润率（%）',
            precision: 2,
          },
          {
            key: 'meltingIRR',
            name: '非融IRR（%）',
            precision: 2,
          },
          {
            key: 'meltingInvestment',
            name: '非融静态投资回收期（月）',
            precision: 0,
          },
        ];

        coreIndicatorsData.forEach(n => {
          const setN = n;
          utils.targetTypes.forEach(j => {
            setN[`${j.key}_${n.key}`] = originalData[j.key][n.key];
          });
        });

        yield put({
          type: 'save',
          payload: {
            coreIndicatorsLoading: false,
            coreIndicatorsData,
          },
        });
      } else {
        // 关闭状态
        yield put({
          type: 'save',
          payload: {
            coreIndicatorsVisible: false,
            coreIndicatorsLoading: false,
          },
        });
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
